import {
    Route,
    Switch,
    Redirect
} from "react-router-dom";
import GlobalConfig from "../config/GlobalConfig";


interface RoutesProps {
    routes: any;
}

function Routes(props: RoutesProps) {
    const { routes } = props;
    let RoutesPage:Array<any> = routes.map((route: any, i: any) => (
        <RouteWithSubRoutes key={i} {...route} />
    ));
    RoutesPage.push( <Redirect to="/" />)
    return (
        <Switch>
            {RoutesPage}
        </Switch>
    );
}

export default Routes;

function RouteWithSubRoutes(route: any) {
    return (
        <Route
            path={route.path}
            render={props => {
                GlobalConfig.history=props.history;
                return (
                // pass the sub-routes down to keep nesting
                <route.component {...props} routes={route.routes} />
            );}}
        />
    );
}

